<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h3>子传父 - {{ count }}</h3>
      <child @fn="fatherfn"></child>
    </div>

    <script src="../Vue.js"></script>
    <script>
      // 子传父
      // 1. 让子组件触发父组件事件
      //    在父组件的子组件标签上面添加自定义事件
      //    在子组件里面要使用this.$emit("自定义事件名称")去触发
      // 2. 传参
      //    this.$emit可以接受多个参数
      const Child = {
        data() {
          return {
            count: 10,
          };
        },
        template: `
          <div>
            child - {{ count }} - <button @click="handleClick">btn</button>
          </div>
        `,
        // mounted() {
        //   this.$emit("fn", 888, this.count);
        // },
        methods: {
          handleClick() {
            // 传递的时候尽量用一个参数传递
            this.$emit("fn", {
              num: 888,
              count: this.count,
            });
          },
        },
      };

      new Vue({
        el: "#app",
        data: {
          count: undefined,
        },
        components: {
          Child,
        },
        methods: {
          fatherfn({ num, count }) {
            console.log("fatherFn", num);
            this.count = count;
          },
        },
      });
    </script>
  </body>
</html>
